(function(angular) {
	'docTemplate strict';
	angular.module('fs.waf').controller('cda.docTemplate.updateController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'docTemplateService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, docTemplateService) {
		var ctrl = this, docTemplateInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.docTemplate = {};
		
		ctrl.init = function() {
			//init dTempParms
			docTemplateService.initParmOptions($scope, ctrl);
			// init docTemplate Parameter
			docTemplateService.initDataOptions($scope, ctrl);
			
			authService.update(docTemplateInfo).then(function(response) {
				ctrl.docTemplate = response.data || docTemplateInfo;
				ctrl.docTemplate.siteCode = storageService.getUserInfo().siteCode;
				ctrl.docTemplate.docTempPreParms = ctrl.docTemplate.docTempPreParms || [];
				ctrl.docTemplate.docTempPostParms = ctrl.docTemplate.docTempPostParms || [];
				docTemplateService.setDocPrePoParm($scope, ctrl);
				
			});
		};
		
		ctrl.updateSave = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			ctrl.docTemplate.docTemplateDocList = ctrl.docTemplate.docTemplateDocList || [];
			if (ctrl.docTemplate.docTemplateDocList && ctrl.docTemplate.docTemplateDocList.length > 0) {				
				ctrl.docTemplate.docId = ctrl.docTemplate.docTemplateDocList[0].docId;
			}
			if (ctrl.docTemplate.taskId) {
				return authService.updateContinueSave(ctrl.docTemplate).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.docTemplate).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			ctrl.docTemplate.docTemplateDocList = ctrl.docTemplate.docTemplateDocList || [];
			if (ctrl.docTemplate.docTemplateDocList && ctrl.docTemplate.docTemplateDocList.length > 0) {				
				ctrl.docTemplate.docId = ctrl.docTemplate.docTemplateDocList[0].docId;
			}
			if (ctrl.docTemplate.taskId) {
				return authService.updateContinueSubmit(ctrl.docTemplate).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.docTemplate).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.docTemplate.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				ctrl.docTemplate = response.data.model;
			}
			if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);
